/// 下载记录数据模型
class DownloadRecord {
  final int? id;
  final int userId;
  final String deviceId;
  final String deviceName;
  final String channelId;
  final String channelName;
  final String videoUrl;
  final String picUrl;
  final String? localPath;
  final String fileName;
  final String startTime;
  final String endTime;
  final String duration;
  final String fileSize;
  final String downloadedSize;
  final double progress;
  final String speed;
  final DownloadStatus status;
  final DateTime createdAt;
  final DateTime? updatedAt;
  final String? errorMessage;

  DownloadRecord({
    this.id,
    required this.userId,
    required this.deviceId,
    required this.deviceName,
    required this.channelId,
    required this.channelName,
    required this.videoUrl,
    required this.picUrl,
    this.localPath, // 改为可选，因为下载开始时可能还没有本地路径
    required this.fileName,
    required this.startTime,
    required this.endTime,
    required this.duration,
    required this.fileSize,
    required this.downloadedSize,
    required this.progress,
    required this.speed,
    required this.status,
    required this.createdAt,
    this.updatedAt,
    this.errorMessage,
  });

  /// 从Map创建DownloadRecord
  factory DownloadRecord.fromMap(Map<String, dynamic> map) {
    final localPathValue = map['local_path'] as String?;
    final localPath =
        localPathValue != null && localPathValue.isNotEmpty
            ? localPathValue
            : null;

    return DownloadRecord(
      id: map['id'] as int?,
      userId: map['user_id'] as int? ?? 0,
      picUrl: map['pic_url'] as String,
      deviceId: map['device_id'] as String,
      deviceName: map['device_name'] as String,
      channelId: map['channel_id'] as String,
      channelName: map['channel_name'] as String,
      videoUrl: map['video_url'] as String,
      localPath: localPath,
      fileName: map['file_name'] as String,
      startTime: map['start_time'] as String,
      endTime: map['end_time'] as String,
      duration: map['duration'] as String,
      fileSize: map['file_size'] as String,
      downloadedSize: map['downloaded_size'] as String,
      progress: (map['progress'] as num).toDouble(),
      speed: map['speed'] as String,
      status: DownloadStatus.values.firstWhere(
        (e) => e.toString() == 'DownloadStatus.${map['status']}',
        orElse: () => DownloadStatus.waiting,
      ),
      createdAt: DateTime.parse(map['created_at'] as String),
      updatedAt:
          map['updated_at'] != null
              ? DateTime.parse(map['updated_at'] as String)
              : null,
      errorMessage: map['error_message'] as String?,
    );
  }

  /// 转换为Map
  Map<String, dynamic> toMap() {
    final map = <String, dynamic>{
      'id': id,
      'user_id': userId,
      'device_id': deviceId,
      'device_name': deviceName,
      'channel_id': channelId,
      'channel_name': channelName,
      'video_url': videoUrl,
      'local_path': localPath ?? '', // 确保不为null，只保存文件路径
      'file_name': fileName,
      'start_time': startTime,
      'pic_url': picUrl,
      'end_time': endTime,
      'duration': duration,
      'file_size': fileSize,
      'downloaded_size': downloadedSize,
      'progress': progress,
      'speed': speed,
      'status': status.toString().split('.').last,
      'created_at': createdAt.toIso8601String(),
    };

    // 只添加非null的字段
    if (updatedAt != null) {
      map['updated_at'] = updatedAt!.toIso8601String();
    }
    if (errorMessage != null) {
      map['error_message'] = errorMessage;
    }

    return map;
  }

  /// 复制并更新字段
  DownloadRecord copyWith({
    int? id,
    int? userId,
    String? deviceId,
    String? deviceName,
    String? channelId,
    String? channelName,
    String? videoUrl,
    String? localPath,
    String? fileName,
    String? startTime,
    String? endTime,
    String? duration,
    String? fileSize,
    String? downloadedSize,
    double? progress,
    String? speed,
    DownloadStatus? status,
    DateTime? createdAt,
    DateTime? updatedAt,
    String? picUrl,
    String? errorMessage,
  }) {
    return DownloadRecord(
      id: id ?? this.id,
      userId: userId ?? this.userId,
      deviceId: deviceId ?? this.deviceId,
      deviceName: deviceName ?? this.deviceName,
      channelId: channelId ?? this.channelId,
      channelName: channelName ?? this.channelName,
      videoUrl: videoUrl ?? this.videoUrl,
      localPath: localPath ?? this.localPath,
      fileName: fileName ?? this.fileName,
      startTime: startTime ?? this.startTime,
      endTime: endTime ?? this.endTime,
      duration: duration ?? this.duration,
      fileSize: fileSize ?? this.fileSize,
      downloadedSize: downloadedSize ?? this.downloadedSize,
      progress: progress ?? this.progress,
      speed: speed ?? this.speed,
      status: status ?? this.status,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      picUrl: picUrl ?? this.picUrl,
      errorMessage: errorMessage ?? this.errorMessage,
    );
  }
}

/// 下载状态枚举
enum DownloadStatus {
  waiting, // 等待中
  downloading, // 下载中
  paused, // 暂停
  completed, // 已完成
  failed, // 失败
}
